package ordercode;

import java.util.Arrays;

public class InsertOrder {
    public static void main(String[] args) {
        // 插入排序，有序无序，然后将无序的数据一个个的与有序的数据对比
        int[] arr = {37,34,13,56,4,67,87,5,43};
        insertion(arr);

    }
    public static void insertion(int[] arr){
        int startIndex = -1; // 有序的开始索引
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]>arr[i+1]){
                startIndex = i+1;
                break;
            }
        }
        for (int i = startIndex; i < arr.length; i++) {
            int j = i;
            // 向前比较
            while (j>0 && arr[j]<arr[j-1]){
                int temp = arr[j];
                arr[j] = arr[j-1];
                arr[j-1] = temp;
                j--;
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}
